﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace Xceed.Silverlight.DataGrid
{
  [ContentProperty( "Child" )]
  internal abstract class CustomBorder : Panel
  {
    #region Child Property

    public UIElement Child
    {
      get
      {
        return m_child;
      }
      set
      {
        if( value == m_child )
          return;

        m_child = value;

        this.SetChildren();
      }
    }

    private void SetChildren()
    {
      if( m_child != null )
      {
        int count = this.Children.Count;

        if( count != 0 )
        {
          Debug.Assert( count == 1 );

          this.Children[ 0 ] = m_child;
        }
        else
        {
          this.Children.Add( m_child );
        }
      }
      else
      {
        this.Children.Clear();
      }
    }

    private UIElement m_child; //null

    #endregion

    #region Projection Property

#if !SILVERLIGHT
    public Projection Projection
    {
      get;
      set;
    }
#endif

    #endregion

    protected override Size MeasureOverride( Size availableSize )
    {
      var child = this.Child;
      Size desiredSize;

      if( child != null )
      {
        child.Measure( availableSize );
        desiredSize = child.DesiredSize;
      }
      else
      {
        desiredSize = new Size( 0d, 0d );
      }

#if LOG_LAYOUT
      Debug.WriteLine( "MeasureOverride (border) as={0}, ds={1}, for={2}", availableSize, desiredSize, this );
#endif

      return desiredSize;
    }

    protected override Size ArrangeOverride( Size finalSize )
    {
      var child = this.Child;

      if( child != null )
      {
        child.Arrange( new Rect( new Point( 0d, 0d ), finalSize ) );
      }

#if LOG_LAYOUT
      Debug.WriteLine( "ArrangeOverride (border) fs={0} for={1}", finalSize, this );
#endif

      return finalSize;
    }
  }
}
